package com.frxuexi.park2209.mynews

import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.frxuexi.base.entity.MyNewsBeanItem
import com.frxuexi.base.mvvm.BaseViewModel
import com.frxuexi.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import javax.inject.Inject

@HiltViewModel
class MyNewsViewModel @Inject constructor(val apiService: ApiService):BaseViewModel() {
    var addnewsSuc=MutableStateFlow<String>("")

    fun getNews():Flow<PagingData<MyNewsBeanItem>>{
        return Pager(config = PagingConfig(10), pagingSourceFactory = {
            MyNewsDataSource(apiService)
        }).flow.cachedIn(viewModelScope)
    }


    fun addNews(map: Map<String,String>){
        httpRequest({apiService.addNews(mapToBody(map))},{addnewsSuc.value=it})
    }
}